"""
Case Type   : ?相关操作符
Case Name   : b_compatibility_mode参数关闭时，使用?相关操作符，正常
Create At   : 2024-10-21
Owner       : @peilinqian
Description :
    1、设置dolphin.b_compatibility_mode参数为off，使用?相关操作符;
Expect      :
    1、设置dolphin.b_compatibility_mode参数为off，使用?相关操作符;expect:成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class OpreatorTest(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbBmode')
        self.user_node = Node('PrimaryDbBmode')
        self.constant = Constant()

    def test_dolphin(self):
        sql_set_cmd = f'set dolphin.b_compatibility_mode to off;'
        text = '----step1:使用?相关操作符;expect:成功----'
        self.log.info(text)
        sql_cmd1 = sql_set_cmd + "SELECT jsonb '{{\\\"a\\\":null, " \
                                 "\\\"b\\\":\\\"qq\\\"}}' ? 'a';"
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1)
        self.log.info(sql_res1)
        check_res = sql_res1.splitlines()[-2].strip().lower()
        self.assertEqual("t", check_res, '执行失败' + text)

        sql_cmd2 = sql_set_cmd + "SELECT jsonb '{{\\\"a\\\":null, " \
                                 "\\\"b\\\":\\\"qq\\\"}}' ?| ARRAY['a','b'];"
        sql_res2 = self.sh_primary.execut_db_sql(sql_cmd2)
        self.log.info(sql_res2)
        check_res = sql_res2.splitlines()[-2].strip().lower()
        self.assertEqual("t", check_res, '执行失败' + text)

        sql_cmd3 = sql_set_cmd + "SELECT jsonb '{{\\\"a\\\":null, " \
                                 "\\\"b\\\":\\\"qq\\\"}}' ?& ARRAY['a','b'];"
        sql_res3 = self.sh_primary.execut_db_sql(sql_cmd3)
        self.log.info(sql_res3)
        check_res = sql_res3.splitlines()[-2].strip().lower()
        self.assertEqual("t", check_res, '执行失败' + text)

        sql_cmd4 = sql_set_cmd + "SELECT lseg '((-1,0),(1,0))' ?# box " \
                                 "'((-2,-2),(2,2))' AS RESULT;"
        sql_res4 = self.sh_primary.execut_db_sql(sql_cmd4)
        self.log.info(sql_res4)
        check_res = sql_res4.splitlines()[-2].strip().lower()
        self.assertEqual("t", check_res, '执行失败' + text)

        sql_cmd5 = sql_set_cmd + f"SELECT ?- lseg '((-1,0),(1,0))' AS RESULT;"
        sql_res5 = self.sh_primary.execut_db_sql(sql_cmd5)
        self.log.info(sql_res5)
        check_res = sql_res5.splitlines()[-2].strip().lower()
        self.assertEqual("t", check_res, '执行失败' + text)

        sql_cmd6 = sql_set_cmd + f"SELECT point '(1,0)' ?- point '(0,0)' " \
                                 f"AS RESULT;"
        sql_res6 = self.sh_primary.execut_db_sql(sql_cmd6)
        self.log.info(sql_res6)
        check_res = sql_res6.splitlines()[-2].strip().lower()
        self.assertEqual("t", check_res, '执行失败' + text)

        sql_cmd7 = sql_set_cmd + f"SELECT ?| lseg '((-1,0),(1,0))' AS RESULT;"
        sql_res7 = self.sh_primary.execut_db_sql(sql_cmd7)
        self.log.info(sql_res7)
        check_res = sql_res7.splitlines()[-2].strip().lower()
        self.assertEqual("f", check_res, '执行失败' + text)

        sql_cmd8 = sql_set_cmd + f"SELECT lseg '((0,0),(0,1))' ?-| lseg " \
                                 f"'((0,0),(1,0))' AS RESULT;"
        sql_res8 = self.sh_primary.execut_db_sql(sql_cmd8)
        self.log.info(sql_res8)
        check_res = sql_res8.splitlines()[-2].strip().lower()
        self.assertEqual("t", check_res, '执行失败' + text)

        sql_cmd9 = sql_set_cmd + f"SELECT lseg '((-1,0),(1,0))' ?|| lseg " \
                                 f"'((-1,2),(1,2))' AS RESULT;"
        sql_res9 = self.sh_primary.execut_db_sql(sql_cmd9)
        self.log.info(sql_res9)
        check_res = sql_res9.splitlines()[-2].strip().lower()
        self.assertEqual("t", check_res, '执行失败' + text)

    def tearDown(self):
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
